"""
Copyright (c) 2023 - present SKbioinfo
"""
from flask_restful import Resource
from flask import request,jsonify,current_app
from apps.API.utils.log_helper import lg
from flask_jwt_extended import jwt_required
import pandas as pd
import os
'''URL:/api/resultsDisplay/FetchSamples/<string:id>'''
class interfaceFetchSamplesByID(Resource):
    @jwt_required()
    def post(self,id):
        try:
            base_dir=current_app.config.get("BASE_DIR")
            SampleFilePath=os.path.join(
                f'{base_dir}results/{id}/','bbmap_covstats_genome.csv',
            )
            infos=[]
            if os.path.exists(SampleFilePath):
                df=pd.read_csv(SampleFilePath)
                for id,row in df.iterrows():
                    tmp={}
                    for key,value in row.items():
                        tmp[key]=value
                    infos.append(tmp)
                response=jsonify(infos)
                response.status=202
                return response       
            else:
                response=jsonify([])
                response.status=300
                return response
        except Exception as e:
            lg.error(e)
            return 404
        
       

        
